<?php
class Merchant_QualityControlController extends Zend_Controller_Action {
	public function preDispatch(){
		$this->tplDirectory = "merchant/qualitycontrol/";
	}
	public function indexAction() {}

	public function listAction() {	
		$qco_status = $this->_request->getParam('qco_status','');
        if($qco_status===''){
            $qco_status=2;
        }
		$this->view->qc_status = $qco_status;
        $data =array('ask'=>0,'total'=>0,'tabselect'=>'');
		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			if(!$customer_id){
                die(json_encode($data));
            }
			
			$cond = array();
			$cond['customer_id'] = $customer_id; 
			$qco_code = $this->_request->getParam('qc_code','');
			$qco_ref_code = $this->_request->getParam('asn_code','');
			$dateFrom = $this->_request->getParam('dateFrom','');
			$dateTo = $this->_request->getParam('dateTo','');
			$qco_status = $this->_request->getParam('qco_status','');
			
			$qco_code && $cond['qco_code'] = $qco_code;
			$qco_ref_code && $cond['qco_ref_code'] = $qco_ref_code;
			$dateFrom && $cond['dateFrom'] = $dateFrom;
			$dateTo && $cond['dateTo'] = $dateTo;			
			if($qco_status!='') $cond['qco_status'] = $qco_status;
			
			$page = $this->_request->getParam('page',1);
			$pageSize = $this->_request->getParam('pageSize',20);
			$page = $page?$page:1;

            $tmpcond=$cond;
			//计算各个状态总数 Draft Confirm finish problem delete
			$tmpcond['qco_status']='0';
			$draftTotal = Merchant_Service_QualityControlOrder::getByCond($tmpcond,'count(*)','qco_id',0,0);
            $tmpcond['qco_status']=1;
			$confirmTotal = Merchant_Service_QualityControlOrder::getByCond($tmpcond,'count(*)','qco_id',0,0);
            $tmpcond['qco_status']=2;
			$finishTotal = Merchant_Service_QualityControlOrder::getByCond($tmpcond,'count(*)','qco_id',0,0);
            $tmpcond['qco_status']=3;
			$problemTotal = Merchant_Service_QualityControlOrder::getByCond($tmpcond,'count(*)','qco_id',0,0);
            $tmpcond['qco_status']=4;
			$deleteTotal = Merchant_Service_QualityControlOrder::getByCond($tmpcond,'count(*)','qco_id',0,0);

			$data['qcostatusTotal'] = array(
							"draftTotal" => $draftTotal,
							"confirmTotal" => $confirmTotal,
							"finishTotal" => $finishTotal,
							"problemTotal" => $problemTotal,
							"deleteTotal" => $deleteTotal );
							
			$total = Merchant_Service_QualityControlOrder::getByCond($cond,'count(*)','qco_id',0,0);
			if($total==0) die(json_encode($data));
            if($qco_status==''){
                $qco_status=0;
                foreach($data['qcostatusTotal'] as $key => $val){
                    if($val>0){
                        $cond['qco_status']=$qco_status;
                        $data['tabselect'] = $qco_status;
                        $total=$val; //当前分类总数
                       break;
                    }
                    $qco_status++;
                }
            }
			$rows = Merchant_Service_QualityControlOrder::getByCond($cond,'*','qco_id',$pageSize,($page-1)*$pageSize);	
			$data['ask'] = 1;
            $data['total'] = $total;
			$data['result'] = $rows;
			die(json_encode($data));	
		}
		echo Cff::renderTpl($this->tplDirectory."list.tpl","user-layout");	
	}
	
	public function detailAction(){
		$qco_code = $this->_request->getParam('qco_code','');	
		$res = Merchant_Service_QualityControlOrder::getDetailResult($qco_code);
		die(json_encode($res));
	}
		
	public function viewPictureAction(){
		$productId = $this->_request->id;
		$url = Merchant_Service_QualityControlOrder::getPicture($productId);
		header("content-type:image/jpeg");
		if($url !== false){
			echo file_get_contents($url);
		}else{
			echo file_get_contents("images/noimg.jpg");
		}
	}
}
?>